﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WLMCoreLibrary;
using MiWebSite_Core;
using Newtonsoft.Json;

namespace MWS_Blog.Edit
{
    

    public partial class Default : System.Web.UI.Page
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
                Response.ContentType = "text/xml";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
            if (CurrentUser.IdUsuario != 0)
            {
                WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();
                dbConnector ConexionDB = new dbConnector(config.Connections[0].value);
                try
                {
                    bool newEntryIndicator = false;
                    string nombreEntry = Request["urlEntryEdit"];
                    string tituloEntry = Request["tituloContenido"];
                    string contenido = Request["textoEdicionContenido"];
                    string contenidoExtra = Request["textoEdicionContenidoExtra"];
                    MiWebSiteBlogEntry blogEntry = new MiWebSiteBlogEntry(nombreEntry, ref ConexionDB);
                    if (blogEntry.id == 0)
                    {
                        blogEntry = new MiWebSiteBlogEntry(ref ConexionDB);
                        blogEntry.PostName = MiWebSiteFunctions.cleanContentName(nombreEntry);
                        newEntryIndicator = true;
                    }
                    if (tituloEntry != blogEntry.titulo)
                    {
                        blogEntry.titulo = tituloEntry;
                        blogEntry.PostName = MiWebSiteFunctions.tituloaPostName(tituloEntry);
                    }
                    blogEntry.contenido = contenido;
                    blogEntry.contenidoExtra = contenidoExtra;
                    blogEntry.ListaCategorias = Server.UrlDecode(Request["listaCats[]"]);
                    blogEntry.ListaTags = Server.UrlDecode(Request["listaTags[]"]);
                    blogEntry.Save(true);
                    try
                    {
                        if (CurrentDomain.oauth_token != "")
                        {
                            Twitter tw = new Twitter();
                            string shorURL = Bitly.Shortener(blogEntry.URLEntry);
                            tw.Update(shorURL + " - " + blogEntry.titulo);
                        }
                    }
                    catch { }
                    Response.Write(WLMFunctions.SerializeObject(new SuccessObject(newEntryIndicator?SuccessCode.RegistrySaved:SuccessCode.RegistryUpdated,"",WLMFunctions.SerializeObject(blogEntry)),true));
                }
                catch (Exception ex)
                {
                    Response.Write(WLMFunctions.SerializeObject(new ErrorObject(ErrorCode.CompilationError,"",ex.Message,ex.StackTrace),true));
                }
                ConexionDB.CloseConnection();
            }
            else
            {
                Response.Write(WLMFunctions.SerializeObject(new ErrorObject(ErrorCode.UserNotLogged),true));
            }
            Response.End();
            

        }
    }
}